/**
 * Created by zhangligang
 * Email: tzhang2450@163.com
 * Date: 2019/1/19
 * Time: 23:41
 */
$(function () {
    var url = '/v1/manager/user/score/file.json';

    var year = $('.select-year').val();

    loadList(url, year);

    getUserLevel();

    $(document).on({
        change: function () {
            refreshList();
        }
    },'.select-status');
    $(document).on('click', '.btn-save-level', function () {
        var data = {
            user_id: uid,
            level: $('.user-level').val()
        };
        if ($('.user-level-id').val() != undefined) {
            data.id = $('.user-level-id').val();
        }
        saveData('/v1/manager/save/user/level', {params: data}, '', '评定完成');
    });
    $(document).on('click', '.btn-change-level', function () {
        var level = parseInt($(this).attr('data-level'));
        var id = parseInt($(this).attr('data-id'));
        var html = '<div class="col-xs-8"><input type="hidden" class="user-level-id" value="'+id+'"><select class="form-control user-level">';
        var level_selected = level == 4 ? ' selected="selected"' : '';
        html += '<option value="4"'+level_selected+'>优秀</option>';
        level_selected = level == 3 ? ' selected="selected"' : '';
        html += '<option value="3"'+level_selected+'>合格</option>';
        level_selected = level == 2 ? ' selected="selected"' : '';
        html += '<option value="2"'+level_selected+'>基本合格</option>';
        level_selected = level == 1 ? ' selected="selected"' : '';
        html += '<option value="1"'+level_selected+'>不合格</option>';
        html += '</select></div><div class="col-xs-4"><a class="btn btn-sm btn-primary btn-save-level" href="javascript:;">提交</a></div>';
        $(this).parents('.secondary-level').html(html);
    });

    $('.select-year').on('change', function () {
        year = $(this).val();
        loadList(url, year);
    });

    $('#normal-dialog').on('show.bs.modal', function (event) {
        $(this).find('.modal-title').text('积分');
        var modal_body = $(this).find('.modal-body');
        var id = $(event.relatedTarget).data('id');
        sendAjax('/v1/manager/user/score/file/detail.json', 'get', {id: id, user_id: uid}, function (json) {
            var score = json.body.score > 0 ? json.body.score : '';
            var html = '<div class="form-horizontal">';
            html += '<div class="form-group">';
            html += '<label for="inputPassword3" class="col-sm-2 control-label">积分项目</label>';
            html += '<div class="col-sm-10">';
            html += '<input type="hidden" class="file-id" value="'+json.body.id+'">';
            html += '<span class="file-content">'+json.body.content+'</span>';
            html += '</div></div>';
            html += '<div class="form-group">';
            html += '<label for="inputPassword3" class="col-sm-2 control-label">参考分数</label>';
            html += '<div class="col-sm-10">';
            html += '<span class="file-refer-score">'+json.body.refer_score+'</span>';
            html += '</div></div>';
            html += '<div class="form-group">';
            html += '<label for="inputPassword3" class="col-sm-2 control-label">打分</label>';
            html += '<div class="col-sm-10">';
            html += '<input type="text" class="form-control file-score" value="'+score+'">';
            html += '</div></div>';
            html += '</div>';
            modal_body.html(html);
        });
    }).on('hide.bs.modal', function(){
        $(this).find('.modal-body').html('');
    });

    $('.dialog-btn-save').on('click', function () {
        var data = {
            id: $('.file-id').val(),
            uid: uid,
            score: $('.file-score').val()
        };
        saveData('/v1/manager/save/user/score/file/score', {params: data}, '', '打分完成');
    });

});

function loadList(url, year) {
    var score = self_score = junior_score = secondary_score = increase = 0;
    $('.data-table').bootstrapTable({
        url: url,
        ajaxOptions:{
            headers: {"Authorization": $.cookie()._token}
        },
        pagination: false,
        toolbar: '',
        iconSize: "outline",
        pageList: [10, 20, 50, 100],
        search: false,
        sortable: false,
        sortOrder: 'desc',
        striped: true,
        sortStable: true,
        sidePagination: 'server',
        showFooter: true,
        queryParams : function (params) {
            params.uid = uid;
            params.year = year;
            return params;
        },
        icons: {
            refresh: "glyphicon-repeat",
            columns: "glyphicon-list"
        },
        columns: [{
            field: 'id',
            title: '序号',
            formatter: function (value) {
                return value == 0 ? '' : value;
            },
        }, {
            field: 'content',
            title: '积分项目',
            footerFormatter: function (row) {
                $.each(row, function (n, v) {
                    if (v.id > 0) {
                        self_score += v.self_score;
                        junior_score += v.junior_score;
                        secondary_score += v.secondary_score;
                        score += v.score;
                    } else {
                        increase = v.score;
                    }
                });
                self_score = self_score*0.9+increase;
                junior_score = junior_score*0.9+increase;
                secondary_score = secondary_score*0.9+increase;
                score = score*0.9+increase;
            }
        }, {
            field: 'self_score',
            title: '自评分',
            footerFormatter: function () {
                return '总计：'+self_score;
            }
        }, {
            field: 'junior_score',
            title: '党支部评分',
            footerFormatter: function () {
                return '总计：'+junior_score;
            }
        }, {
            field: 'secondary_score',
            title: '党委评分',
            footerFormatter: function () {
                return '总计：'+score;
            }
        }, {
            field: 'score_file_id',
            title: '操作',
            formatter: function (value) {
                return value == undefined ? '' : '<a class="btn btn-sm btn-info" href="#" data-id="'+value+'" data-toggle="modal" data-target="#normal-dialog"><i class="fa fa-edit"></i>打分</a>';
            }
        }]
    });
}

function refreshList() {
    $('.data-table').bootstrapTable('refresh');
}

function getUserLevel() {
    sendAjax('/v1/manager/user/level.json', 'get', {user_id: uid}, function (json) {
        var self_level = json.body.self_level;
        if (self_level.level > 0) {
            var level = getLevelName(self_level.level);
            $('.self-level').html('<div class="col-xs-12">'+level+'</div>');
        }
        var junior_level = json.body.junior_level;
        if (junior_level.level > 0) {
            level = getLevelName(junior_level.level);
            $('.junior-level').html('<div class="col-xs-12">'+level+'</div>');
        }
        var secondary_level = json.body.secondary_level;
        var html = '<div class="col-xs-8"><select class="form-control user-level"><option value="4">优秀</option><option value="3">合格</option><option value="2">基本合格</option><option value="1">不合格</option></select></div><div class="col-xs-4"><a class="btn btn-sm btn-primary btn-save-level" href="javascript:;">提交</a></div>';
        if (secondary_level.level > 0) {
            level = getLevelName(secondary_level.level);
            html = '<div class="col-xs-6">'+level+'</div><div class="col-xs-6"><a class="btn btn-sm btn-success btn-change-level" data-id="'+secondary_level.id+'" data-level="'+secondary_level.level+'" href="javascript:;">修改</a></div>';
        }
        $('.secondary-level').html(html);
    })
}